home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vg-2.03 / jpeg / jpeg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  12.4 KB  |  572 lines

  1. /*
  2.  * Copyright (C) 1992-1993 Michael Davidson.
  3.  * All rights reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software
  6.  * and its documentation for any purpose and without fee is hereby
  7.  * granted, provided that the above copyright notice appear in all
  8.  * copies and that both that copyright notice and this permission
  9.  * notice appear in supporting documentation.
  10.  *
  11.  * This software is provided "as is" without express or implied warranty.
  12.  */
  13.  
  14. /*
  15.  * This software is derived from the Independent JPEG Group's software
  16.  *
  17.  * Copyright (C) 1991, 1992 Thomas G. Lane.
  18.  * This file is part of the Independent JPEG Group's software.
  19.  *
  20.  * For conditions of distribution and use, see the accompanying README file.
  21.  */
  22.  
  23. #include    <stdio.h>
  24. #include    "jpeg.h"
  25. #include    "image.h"
  26.  
  27. STATIC image_t    *JPEGImage;
  28.  
  29. typedef enum {            /* JPEG marker codes */
  30.   M_SOF0  = 0xc0,
  31.   M_SOF1  = 0xc1,
  32.   M_SOF2  = 0xc2,
  33.   M_SOF3  = 0xc3,
  34.   
  35.   M_SOF5  = 0xc5,
  36.   M_SOF6  = 0xc6,
  37.   M_SOF7  = 0xc7,
  38.   
  39.   M_JPG   = 0xc8,
  40.   M_SOF9  = 0xc9,
  41.   M_SOF10 = 0xca,
  42.   M_SOF11 = 0xcb,
  43.   
  44.   M_SOF13 = 0xcd,
  45.   M_SOF14 = 0xce,
  46.   M_SOF15 = 0xcf,
  47.   
  48.   M_DHT   = 0xc4,
  49.   
  50.   M_DAC   = 0xcc,
  51.   
  52.   M_RST0  = 0xd0,
  53.   M_RST1  = 0xd1,
  54.   M_RST2  = 0xd2,
  55.   M_RST3  = 0xd3,
  56.   M_RST4  = 0xd4,
  57.   M_RST5  = 0xd5,
  58.   M_RST6  = 0xd6,
  59.   M_RST7  = 0xd7,
  60.   
  61.   M_SOI   = 0xd8,
  62.   M_EOI   = 0xd9,
  63.   M_SOS   = 0xda,
  64.   M_DQT   = 0xdb,
  65.   M_DNL   = 0xdc,
  66.   M_DRI   = 0xdd,
  67.   M_DHP   = 0xde,
  68.   M_EXP   = 0xdf,
  69.   
  70.   M_APP0  = 0xe0,
  71.   M_APP1  = 0xe1,
  72.   M_APP2  = 0xe2,
  73.   M_APP3  = 0xe3,
  74.   M_APP4  = 0xe4,
  75.   M_APP5  = 0xe5,
  76.   M_APP6  = 0xe6,
  77.   M_APP7  = 0xe7,
  78.   M_APP8  = 0xe8,
  79.   M_APP9  = 0xe9,
  80.   M_APP10 = 0xea,
  81.   M_APP11 = 0xeb,
  82.   M_APP12 = 0xec,
  83.   M_APP13 = 0xed,
  84.   M_APP14 = 0xee,
  85.   M_APP15 = 0xef,
  86.   
  87.   M_JPG0  = 0xf0,
  88.   M_JPG1  = 0xf1,
  89.   M_JPG2  = 0xf2,
  90.   M_JPG3  = 0xf3,
  91.   M_JPG4  = 0xf4,
  92.   M_JPG5  = 0xf5,
  93.   M_JPG6  = 0xf6,
  94.   M_JPG7  = 0xf7,
  95.   M_JPG8  = 0xf8,
  96.   M_JPG9  = 0xf9,
  97.   M_JPG10 = 0xfa,
  98.   M_JPG11 = 0xfb,
  99.   M_JPG12 = 0xfc,
  100.   M_JPG13 = 0xfd,
  101.   M_COM   = 0xfe,
  102.   
  103.   M_TEM   = 0x01,
  104.   
  105. } JPEG_MARKER;
  106.  
  107. STATIC int    jpegNextMarker(FILE *);
  108. STATIC void    jpegSkipMarker(FILE *, int);
  109.  
  110. /*
  111.  * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
  112.  * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
  113.  * The conversion equations to be implemented are therefore
  114.  *    R = Y                + 1.40200 * Cr
  115.  *    G = Y - 0.34414 * Cb - 0.71414 * Cr
  116.  *    B = Y + 1.77200 * Cb
  117.  * where Cb and Cr represent the incoming values less MAXJSAMPLE/2.
  118.  * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
  119.  *
  120.  * To avoid floating-point arithmetic, we represent the fractional constants
  121.  * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
  122.  * the products by 2^16, with appropriate rounding, to get the correct answer.
  123.  * Notice that Y, being an integral input, does not contribute any fraction
  124.  * so it need not participate in the rounding.
  125.  *
  126.  * For even more speed, we avoid doing any multiplications in the inner loop
  127.  * by precalculating the constants times Cb and Cr for all possible values.
  128.  * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
  129.  * for 12-bit samples it is still acceptable.  It's not very reasonable for
  130.  * 16-bit samples, but if you want lossless storage you shouldn't be changing
  131.  * colorspace anyway.
  132.  * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
  133.  * values for the G calculation are left scaled up, since we must add them
  134.  * together before rounding.
  135.  */
  136.  
  137. #define SCALEBITS    16    /* speedier right-shift on some machines */
  138. #define ONE_HALF    (1L << (SCALEBITS-1))
  139. #define FIX(x)        ((long) ((x) * (1L<<SCALEBITS) + 0.5))
  140.  
  141. int Cr_r_tab[256];    /* => table for Cr to R conversion */
  142. int Cb_b_tab[256];    /* => table for Cb to B conversion */
  143. int Cr_g_tab[256];    /* => table for Cr to G conversion */
  144. int Cb_g_tab[256];    /* => table for Cb to G conversion */
  145.  
  146. /*
  147.  * Initialize for colorspace conversion.
  148.  */
  149.  
  150. ycc_rgb_init()
  151. {
  152.     register int    i, x2;
  153.  
  154.     for (i = 0; i < 256; i++)
  155.     {
  156.     /* i is the actual input pixel value, in the range 0..MAXJSAMPLE */
  157.     /* The Cb or Cr value we are thinking of is x = i - MAXJSAMPLE/2 */
  158.     x2 = 2*i - 255;    /* twice x */
  159.     /* Cr=>R value is nearest int to 1.40200 * x */
  160.     Cr_r_tab[i] = (int) (FIX(1.40200/2) * x2 + ONE_HALF) >> SCALEBITS;
  161.     /* Cb=>B value is nearest int to 1.77200 * x */
  162.     Cb_b_tab[i] = (int) (FIX(1.77200/2) * x2 + ONE_HALF) >> SCALEBITS;
  163.     /* Cr=>G value is scaled-up -0.71414 * x */
  164.     Cr_g_tab[i] = (- FIX(0.71414/2)) * x2;
  165.     /* Cb=>G value is scaled-up -0.34414 * x */
  166.     /* We also add in ONE_HALF so that need not do it in inner loop */
  167.     Cb_g_tab[i] = (- FIX(0.34414/2)) * x2 + ONE_HALF;
  168.     }
  169. }
  170.  
  171. #if !defined(ASM)
  172. ycc_to_rgb(
  173.     unsigned char    *y_ptr,
  174.     unsigned char    *cb_ptr,
  175.     unsigned char    *cr_ptr,
  176.     unsigned char    *rgb_ptr,
  177.     int            count
  178.     )
  179. {
  180.     register int    cb, cr;
  181.     extern unsigned char    *range_limit_0_255;
  182.  
  183.     while (--count >= 0)
  184.     {
  185.     register unsigned char    *range_limit;
  186.  
  187.     range_limit    = &range_limit_0_255[*y_ptr++];
  188.     cb        = *cb_ptr++;
  189.     cr        = *cr_ptr++;
  190.  
  191.     /* red */
  192.     *rgb_ptr++    = range_limit[Cr_r_tab[cr]];
  193.     /* green */
  194.     *rgb_ptr++    = range_limit[(Cb_g_tab[cb]+Cr_g_tab[cr]) >> SCALEBITS];
  195.     /* blue */
  196.     *rgb_ptr++    = range_limit[Cb_b_tab[cb]];
  197.      }
  198. }
  199. #endif
  200.  
  201. int
  202. jpegPutPixelsYCC(
  203.     int        col,
  204.     int        row,
  205.     int        width,
  206.     int        height,
  207.     JSAMPLE    ***pixel_data
  208.     )
  209. {
  210.     register int    i;
  211.  
  212.     for (i = 0; i < height; i++)
  213.     ycc_to_rgb(pixel_data[0][i], pixel_data[1][i], pixel_data[2][i],
  214.         JPEGImage->pixels[row+i], width);
  215.     return imageRefresh(col, row, width, height);
  216. }
  217.  
  218. int
  219. jpegPutPixelsGS(
  220.     int        col,
  221.     int        row,
  222.     int        width,
  223.     int        height,
  224.     JSAMPLE    ***pixel_data
  225.     )
  226. {
  227.     int            i;
  228.     int            r;
  229.  
  230.     for (i = 0; i < height; i++)
  231.     if ((r = imagePutPixels(col, row++, pixel_data[0][i], width)) != 0)
  232.         return r;
  233.  
  234.     return 0;
  235. }
  236.  
  237. jpegError(format, a0)
  238.      char *format;
  239. {
  240.     return imageError(format, a0);
  241. }
  242.  
  243. char    *
  244. jpegInfo(
  245.     FILE    *fp
  246.     )
  247. {
  248.     int        m;
  249.     int        precision, width, height, num_components;
  250.     static char    info[80];
  251.     char    *p;
  252.  
  253.     /* Expect an SOI marker first */
  254.     if (jpegNextMarker(fp) != M_SOI)
  255.     return NULL;
  256.  
  257.     while ((m = jpegNextMarker(fp)) != M_EOI && m != EOF)
  258.     {
  259.     switch (m)
  260.     {
  261.         case M_SOF0:  case M_SOF1:  case M_SOF2:  case M_SOF3:
  262.         case M_SOF5:  case M_SOF6:  case M_SOF7:  case M_SOF9:
  263.         case M_SOF10: case M_SOF11: case M_SOF13: case M_SOF14:
  264.         case M_SOF15:
  265.         (void) jpegGetWord(fp);
  266.         precision    = getc(fp);
  267.         height        = jpegGetWord(fp);
  268.         width        = jpegGetWord(fp);
  269.         num_components    = getc(fp);
  270.         if (num_components == 1)
  271.             p = "8 bit grayscale";
  272.         else if (num_components == 3)
  273.             p = "24 bit color";
  274.         else
  275.             p = "unknown color space";
  276.         sprintf(info, "JPEG %dx%d %s", width, height, p);
  277.         return info;
  278.           
  279.         case M_RST0: case M_RST1: case M_RST2: case M_RST3:
  280.         case M_RST4: case M_RST5: case M_RST6: case M_RST7:
  281.         break;
  282.     
  283.         default:
  284.         jpegSkipMarker(fp, m);
  285.         break;
  286.     }
  287.     }
  288.  
  289.     return NULL;
  290. }
  291.  
  292. jpegRead(
  293.     char    *name,
  294.     FILE    *fp
  295.     )
  296. {
  297.     int        m;
  298.     int        r = 0;
  299.     struct decompress_info_struct    cinfo;
  300.     JPEG_TABLES    jtables;
  301.     int        restart_interval;
  302.  
  303.     /* Expect an SOI marker first */
  304.     if (jpegNextMarker(fp) != M_SOI)
  305.     return jpegError("File does not start with JPEG SOI marker", 0);
  306.   
  307.     restart_interval = 0;
  308.  
  309.     while ((m = jpegNextMarker(fp)) != M_EOI && m != EOF && r == 0)
  310.     {
  311.     switch (m)
  312.     {
  313.         case M_SOF0:
  314.         case M_SOF1:  case M_SOF2:  case M_SOF3:  case M_SOF5:
  315.         case M_SOF6:  case M_SOF7:  case M_SOF9:  case M_SOF10:
  316.         case M_SOF11: case M_SOF13: case M_SOF14: case M_SOF15:
  317.         r = jpegSOF(fp, m, &cinfo);
  318.         break;
  319.           
  320.         case M_DHT:
  321.         r = jpegDHT(fp, &jtables);
  322.         break;
  323.           
  324.         case M_SOS:
  325.         if ((r = jpegSOS(fp, &cinfo)) != 0)
  326.             break;
  327.  
  328.         if (cinfo.comps_in_scan == 1)
  329.         {
  330.             if ((JPEGImage = imageStart(name, cinfo.image_width,
  331.             cinfo.image_height, 8, 0)) == NULL)
  332.             {
  333.             r = imageError("can't allocate %dx%d 8 bit JPEG image",
  334.                 cinfo.image_width, cinfo.image_height);
  335.             break;
  336.             }
  337.             
  338.             imageSetGrayColorMap(255);
  339.             imageShowInfo(1);
  340.  
  341.             r = jpegScanDecode(fp, &cinfo, &jtables, restart_interval,
  342.             jpegPutPixelsGS);
  343.         }
  344.         else if (cinfo.comps_in_scan == 3)    /* color (YCbCr) */
  345.         {
  346.             ycc_rgb_init();
  347.             if ((JPEGImage = imageStart(name, cinfo.image_width,
  348.             cinfo.image_height, 24, 0)) == NULL)
  349.             {
  350.             r = imageError("can't allocate %dx%d 24 bit JPEG image",
  351.                 cinfo.image_width, cinfo.image_height);
  352.             break;
  353.             }
  354.             
  355.             imageShowInfo(1);
  356.             r = jpegScanDecode(fp, &cinfo, &jtables, restart_interval,
  357.             jpegPutPixelsYCC);
  358.         }
  359.         else
  360.             r = jpegError("unknown JPEG colorspace", 0);
  361.  
  362.         break;
  363.  
  364.         case M_DAC:
  365.         r = jpegError("Arithmetic coding not supported", 0);
  366.         break;
  367.           
  368.         case M_DQT:
  369.         r = jpegDQT(fp, &jtables);
  370.         break;
  371.           
  372.         case M_DRI:
  373.         r = jpegDRI(fp, &restart_interval);
  374.         break;
  375.           
  376.         case M_RST0: case M_RST1: case M_RST2: case M_RST3:
  377.         case M_RST4: case M_RST5: case M_RST6: case M_RST7:
  378.         break;
  379.     
  380.         default:
  381.         jpegSkipMarker(fp, m);
  382.         break;
  383.     }
  384.     }
  385.  
  386.     if (r != 0)
  387.     return r;
  388.  
  389.     if (m != M_EOI  && (r = imageWarning("missing EOI marker")) != 0)
  390.     return r;
  391.  
  392.     return imageEnd();
  393. }
  394.  
  395. /*
  396.  * Routines to parse JPEG markers & save away the useful info.
  397.  */
  398.  
  399. STATIC int
  400. jpegSOF(
  401.     FILE        *fp,
  402.     int            marker,
  403.     decompress_info_ptr    cinfo
  404.     )
  405. /* Process a SOFn marker */
  406. {
  407.     int        length;
  408.     int        ci;
  409.     int        c;
  410.     jpeg_component_info    *compptr;
  411.   
  412.     if (marker != M_SOF0)
  413.     return jpegError("unsupported SOF marker %02x", marker);
  414.  
  415.     length = jpegGetWord(fp);
  416.   
  417.     cinfo->data_precision = getc(fp);
  418.     cinfo->image_height   = jpegGetWord(fp);
  419.     cinfo->image_width    = jpegGetWord(fp);
  420.     cinfo->num_components = getc(fp);
  421.  
  422.   /* We don't support files in which the image height is initially specified */
  423.   /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
  424.   /* might as well have a general sanity check. */
  425.     if (cinfo->image_height <= 0 || cinfo->image_width <= 0 || cinfo->num_components <= 0)
  426.     return jpegError("Empty JPEG image (DNL not supported)", 0);
  427.  
  428.     if (cinfo->data_precision != 8)
  429.     return jpegError("Unsupported JPEG data precision", 0);
  430.  
  431.     if (length != (cinfo->num_components * 3 + 8))
  432.     return jpegError("Bogus SOF length", 0);
  433.  
  434.     for (ci = 0; ci < cinfo->num_components; ci++)
  435.     {
  436.     compptr            = &cinfo->comp_info[ci];
  437.     compptr->component_index= ci;
  438.     compptr->component_id    = getc(fp);
  439.     c            = getc(fp);
  440.     compptr->h_samp_factor    = (c >> 4) & 0x0f;
  441.     compptr->v_samp_factor    = c & 0x0f;
  442.     compptr->quant_tbl_no    = getc(fp);
  443.     }
  444.     return 0;
  445. }
  446.  
  447.  
  448. STATIC int
  449. jpegSOS(
  450.     FILE        *fp,
  451.     decompress_info_ptr cinfo
  452.     )
  453. /* Process a SOS marker */
  454. {
  455.     int        length;
  456.     int i, ci, n, c, cc;
  457.     jpeg_component_info * compptr;
  458.   
  459.     length    = jpegGetWord(fp);
  460.     n        = getc(fp);
  461.     cinfo->comps_in_scan = n;
  462.     length    -= 3;
  463.   
  464.     if (length != (n * 2 + 3) || n < 1 || n > MAX_COMPS_IN_SCAN)
  465.     return jpegError("Bogus SOS length", 0);
  466.  
  467.     for (i = 0; i < n; i++)
  468.     {
  469.     cc    = getc(fp);
  470.     c    = getc(fp);
  471.     length    -= 2;
  472.     
  473.     for (ci = 0; ci < cinfo->num_components; ci++)
  474.         if (cc == cinfo->comp_info[ci].component_id)
  475.         break;
  476.     
  477.     if (ci >= cinfo->num_components)
  478.         return jpegError("Invalid component number in SOS", 0);
  479.     
  480.     compptr            = &cinfo->comp_info[ci];
  481.     cinfo->cur_comp_info[i]    = compptr;
  482.     compptr->dc_tbl_no    = (c >> 4) & 15;
  483.     compptr->ac_tbl_no    = (c     ) & 15;
  484.     }
  485.   
  486.     (void) getc(fp);
  487.     (void) getc(fp);
  488.     (void) getc(fp);
  489.  
  490.     return 0;
  491. }
  492.  
  493. /*
  494.  * jpegDRI()    - process a DRI marker
  495.  */
  496. int
  497. jpegDRI(
  498.     FILE        *fp,
  499.     int            *interval
  500.     )
  501. {
  502.     if (jpegGetWord(fp) != 4)
  503.     return jpegError("Bogus length in DRI", 0);
  504.  
  505.     *interval = jpegGetWord(fp);
  506.  
  507.     return 0;
  508. }
  509.  
  510. /*
  511.  * jpegNextMarker(fp)
  512.  *
  513.  *    - returns next JPEG marker or EOF
  514.  */
  515. STATIC int
  516. jpegNextMarker (
  517.     FILE    *fp
  518.     )
  519. {
  520.     int        c;
  521.  
  522.     do
  523.     {
  524.     while ((c = getc(fp)) != EOF && c != 0xff)
  525.         continue;
  526.  
  527.     while ((c = getc(fp)) != EOF && c == 0xff)
  528.         continue;
  529.     } while (c == 0);
  530.  
  531.     return c;
  532. }
  533.  
  534. /*
  535.  * jpegSkipMarker(fp, m)
  536.  *
  537.  *    - skips a variable length JPEG marker
  538.  */
  539. /*ARGSUSED*/
  540. STATIC void
  541. jpegSkipMarker(
  542.     FILE    *fp,
  543.     int        marker
  544.     )
  545. {
  546.     int        length;
  547.   
  548.     length = jpegGetWord(fp);
  549.   
  550.     for (length -= 2; length > 0; length--)
  551.     (void) getc(fp);
  552. }
  553.  
  554. int
  555. jpegGetWord(
  556.     FILE    *fp
  557.     )
  558. {
  559.     unsigned short    a;
  560.   
  561.     a = getc(fp);
  562.     return (a << 8) + getc(fp);
  563. }
  564.  
  565. int
  566. jpegGetByte(
  567.     FILE    *fp
  568.     )
  569. {
  570.     return getc(fp);
  571. }
  572.